package com.ywdnf.gomoku.util;

import com.ywdnf.gomoku.model.Result;
import com.ywdnf.gomoku.model.chess.Game;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * msg util
 *
 * @author lss
 * @date 2019-02-05, Tue
 */
@SuppressWarnings("unchecked")
@Component
public class MessageUtils {

    private static SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    public void setObjRedisTemplate(SimpMessagingTemplate simpMessagingTemplate) {
        MessageUtils.simpMessagingTemplate = simpMessagingTemplate;
    }

    /**
     * 向某一个用户发送结果
     *
     * @param user   唯一用户, ufid
     * @param result 结果
     */
    public static void sendToUser(String user, Result result) {
        simpMessagingTemplate.convertAndSendToUser(user, "/queue/myself", JsonUtil.toJsonString(result));
    }

    public static void sendToGameUserSameMsg(Game game, Result result) {
        sendToUser(game.getPlayer1().getUserId(), result);
        sendToUser(game.getPlayer2().getUserId(), result);
    }

}
